/*
-----------------------------------------------------------------------------
This source file is part of Labor3D
(Labor 3D Graphics Engine)

Copyright (c) 2009-2020 Rock Mao
* creator : Rock Mao
* created : 2009-10-26   10:33
* filename: LaborD3D10GeometryShader.cpp
-----------------------------------------------------------------------------
*/

#include "LaborD3D10GeometryShader.h"
#include "LaborD3D10RenderSystem.h"
#include "LaborCore.h"
#include "Common/LaborLogger.h"
NAMESPACE_LABOR3D_BEGIN

CLD3D10GeometryShader::CLD3D10GeometryShader()
{
	m_pGeometryShader = NULL;
}

CLD3D10GeometryShader::~CLD3D10GeometryShader()
{
	unload();
}

bool  CLD3D10GeometryShader::installShaderConstTable(ILShaderParameters* pShaderParamBuffer)
{
	return true;
}



bool CLD3D10GeometryShader::_load(const wchar_t* fileName , const unsigned char* buf , size_t bufLen, unsigned long arg)
{
	unload();

	ID3D10Blob* pShaderCode = NULL;
	ID3D10Blob* pErroMsg    = NULL;
	D3D10_SHADER_MACRO* pMacroDef = NULL;
	ID3D10Include*      pInclude  = NULL;
	const char*         func_name = "main";

	DWORD dwShaderFlags = 0;
#if defined( DEBUG ) || defined( _DEBUG )
	dwShaderFlags |= D3D10_SHADER_DEBUG;
#endif

	//ID3DX10ThreadPump* pThreadPump = NULL;
	DWORD dwEffectFlags = 0;
	HRESULT  hr = D3DX10CompileFromMemory((LPCSTR)buf,bufLen,NULL,pMacroDef,pInclude,func_name,"ps_4_0",dwShaderFlags, dwEffectFlags, NULL , &pShaderCode,&pErroMsg , NULL);

	if( FAILED(hr) )
	{
		const char* strErroMsg = (const char*)pErroMsg->GetBufferPointer();
		LABOR_LOG_A(eLLL_ERROR_NORMAL, " >====Shader===%s\n", strErroMsg );

		SafeRelease(pShaderCode);
		SafeRelease(pErroMsg);
		return false;
	}

	CLD3D10RenderSystem *pRender = (CLD3D10RenderSystem *)GetRenderSystem();

	hr = pRender->D3D10Device()->CreateGeometryShader(pShaderCode->GetBufferPointer() , pShaderCode->GetBufferSize() , &m_pGeometryShader);
	if( FAILED(hr) )
	{
		SafeRelease(pShaderCode);
		SafeRelease(pErroMsg);
		return false;
	}

	afterLoad(pShaderCode);
	SafeRelease(pShaderCode);
	SafeRelease(pErroMsg);
	m_Name = fileName;
	return true;
}

bool CLD3D10GeometryShader::isLoaded()
{
	return m_pGeometryShader != NULL;
}

bool CLD3D10GeometryShader::unload()
{
	CLD3D10Shader::unload();
	SafeRelease(m_pGeometryShader);
	return true;
}


NAMESPACE_LABOR3D_END
